import { Inject, Post, Body, Get, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { QrcodeQrcodeEntity } from '../../entity/qrcode';
import { QrcodeQrcodeService } from '../../service/qrcode';
import { BaseSysUserEntity } from '../../../base/entity/sys/user';
import { QrcodeDataboardService } from '../../service/databoard';

/**
 * 二维码信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: QrcodeQrcodeEntity,
  service: QrcodeQrcodeService,
  insertParam: ctx => {
    return {
      adminId: ctx.admin.userId,
    };
  },
  pageQueryOp: {
    keyWordLikeFields: ['a.code'],
    fieldEq: ['a.adminId', 'a.channelId', 'a.status', 'a.type'],
    select: ['a.*', 'b.name as adminName'],
    join: [
      {
        entity: BaseSysUserEntity,
        alias: 'b',
        condition: 'a.adminId = b.id',
        type: 'leftJoin',
      }
    ],
  },
})
export class AdminQrcodeQrcodeController extends BaseController {
  @Inject()
  qrcodeQrcodeService: QrcodeQrcodeService;

  @Inject()
  qrcodeDataboardService: QrcodeDataboardService;

  // 生成二维码
  @Post('/makeQrcode')
  async makeQrcode(@Body() body) {
    const { id } = body;
    const result = await this.qrcodeQrcodeService.makeQrcode(id);
    return result;
  }

  // 根据id获取二维码信息
  @Post('/getQRCodeInfo')
  async getQRCodeInfo(@Body() body) {
    const { id } = body;
    const result = await this.qrcodeQrcodeService.getQRCodeInfo(id);
    return result;
  }

  @Get('/getSalesAppointConvertBoardForDaren', { summary: '达人预约数据' })
  async getSalesAppointConvertBoardForDaren(
    @Query() param: {
      startTime?: string; // 开始时间，可选
      endTime?: string;   // 结束时间，可选
    }
  ) {
    const result = await this.qrcodeDataboardService.getSalesAppointConvertBoardForDaren(param);
    return this.ok(result);
  }

  @Post('/makeShopQrcode')
  async makeShopQrcode(@Body() body) {
    const { shopId } = body;
    const result = await this.qrcodeQrcodeService.makeShopQrcode(shopId);
    return this.ok(result);
  }

  // 生成自定义参数二维码
  @Post('/makeCustomQrcode')
  async makeCustomQrcode(@Body() body) {
    const { scene } = body;
    const result = await this.qrcodeQrcodeService.makeCustomQrcode(scene);
    return this.ok(result);
  }
}
